package com.qianxx.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianxx.base.R;


/**
 * 标题栏
 * <p>可以通过 R.layout.head_view 覆盖标题栏的样式</p>
 * <p>1. app:head</p>
 * <p>
 * Created on 16/5/24 上午9:50.
 *
 * @author binwin20
 */
public class HeadView extends FrameLayout {

    private TextView mTitle;
    private ImageView mLeft;
    private ImageView mRight;
    private TextView mRightTxt;

    public HeadView(Context context) {
        this(context, null);
    }

    public HeadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.head_view, this);
        TypedArray ta = context.getTheme()
                .obtainStyledAttributes(attrs, R.styleable.HeadView, defStyleAttr, R.style.HeadViewDefStyle);

        mTitle = (TextView) findViewById(R.id.tx_head_title);
        mLeft = (ImageView) findViewById(R.id.img_head_left);
        mRight = (ImageView) findViewById(R.id.img_head_right);
        mRightTxt = (TextView) findViewById(R.id.tx_head_right);

        String title = ta.getString(R.styleable.HeadView_head_title);
        mTitle.setText(title);

        int color = ta.getColor(R.styleable.HeadView_text_color, context.getResources().getColor(R.color.text_color));
        mTitle.setTextColor(color);
        mRightTxt.setTextColor(color);

        int leftImg = ta.getResourceId(R.styleable.HeadView_left_image, 0);
        if (leftImg != 0) {
            mLeft.setImageResource(leftImg);
            mLeft.setVisibility(VISIBLE);
        }

        int rightImg = ta.getResourceId(R.styleable.HeadView_right_image, 0);
        if (rightImg != 0) {
            mRight.setImageResource(rightImg);
            mRight.setVisibility(VISIBLE);
            mRightTxt.setVisibility(GONE);
        }

        String rightTxt = ta.getString(R.styleable.HeadView_right_text);
        if (!TextUtils.isEmpty(rightTxt)) {
            mRightTxt.setText(rightTxt);
            mRightTxt.setVisibility(VISIBLE);
            mRight.setVisibility(GONE);
        }

        // 默认结束 Activity
        mLeft.setOnClickListener(view -> ((Activity) context).finish());
        ta.recycle();
    }

    public void setTitle(String title) {
        mTitle.setText(title);
    }

    public void setTitle(@StringRes int title) {
        mTitle.setText(title);
    }

    public void setLeftVisibility(boolean visible) {
        mLeft.setVisibility(visible ? VISIBLE : INVISIBLE);
    }

    public void setRightImageVisibility(boolean visible) {
        mRight.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setRightTxtVisibility(boolean visible) {
        mRightTxt.setVisibility(visible ? VISIBLE : GONE);
    }

    public void setRightTxt(String txt) {
        mRightTxt.setText(txt);
    }

    public void setRightTxt(@StringRes int txt) {
        mRightTxt.setText(txt);
    }

    public View getLeftView() {
        return mLeft;
    }

    public View getRightView() {
        return mRight;
    }

    public View getRightTextView() {
        return mRightTxt;
    }
}
